﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using LumenWorks.Framework.IO.Csv;
using System.IO;

namespace Utilities
{
    public class DataTableUtil
    {
        public static void saveDataTable2File(DataTable dtSource, string strFileCSVDestination)
        {
            CSVUtil.saveDataTableToCSVFile(dtSource, strFileCSVDestination);
        }

        public static DataTable buildDataTableFromFile(string strFileCSVSource)
        {
            CsvReader csvReaderFile = new CsvReader(new StreamReader(strFileCSVSource), true, 2024);
            string[] headers_favouriteFile = csvReaderFile.GetFieldHeaders();
            int fieldCounts_csvReaderFile = csvReaderFile.FieldCount;

            //Clear the list of actions
            DataTable dtTarget = new DataTable();

            for (int i = 0; i < fieldCounts_csvReaderFile; i++)
            {
                dtTarget.Columns.Add(new DataColumn("Column" + i.ToString(), typeof(string)));
            }

            while (csvReaderFile.ReadNextRecord())
            {
                DataRow drowDt = dtTarget.NewRow();

                for (int i = 0; i < fieldCounts_csvReaderFile; i++)
                {
                    string STR = csvReaderFile[i];
                    drowDt[i] = csvReaderFile[i];
                }
                dtTarget.Rows.Add(drowDt);
            }
            csvReaderFile.Dispose();


            return dtTarget;
        }

        public static DataTable buildDataTableFromFileDictionary(string strFileCSVSource)
        {
            CsvReader csvReaderFile = new CsvReader(new StreamReader(strFileCSVSource), true, 2024);
            int fieldCounts_csvReaderFile = csvReaderFile.FieldCount;

            //Clear the list of actions
            DataTable dtTarget = new DataTable();

            for (int i = 0; i < fieldCounts_csvReaderFile; i++)
            {
                dtTarget.Columns.Add(new DataColumn("Column" + i.ToString(), typeof(string)));
            }

            while (csvReaderFile.ReadNextRecord())
            {
                DataRow drowDt = dtTarget.NewRow();

                for (int i = 0; i < fieldCounts_csvReaderFile; i++)
                {
                    string STR = csvReaderFile[i];
                    drowDt[i] = csvReaderFile[i];
                }
                dtTarget.Rows.Add(drowDt);
            }
            csvReaderFile.Dispose();


            return dtTarget;
        }

        public static string getValueByKey(DataTable DT, string strKey)
        {
            for (int i = 0; i < DT.Rows.Count; i++)
            {
                DataRow DR = DT.Rows[i];
                if (DR[0].ToString().Trim().ToLower().Equals(strKey.Trim().ToLower()))
                {
                    return DR[1].ToString();
                }
            }
            return null;
        }

        public static string getValueByRowOrderColumnOrder(DataTable DT, int intOrderOfRow, int intOrderOfColumn)
        {
            DataRow DR = DT.Rows[intOrderOfRow];
            return DR[intOrderOfColumn].ToString();
        }
    }
}
